package jjn.carl.greedy;

import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/3 14:48
 */
public class LeetCode738 {
    public int monotoneIncreasingDigits(int n) {
        String input = String.valueOf(n);
        char[] array = input.toCharArray();
        int flag = input.length();
        for (int i = input.length() - 1; i > 0; i--) {
            if (array[i - 1] > array[i]) {
                array[i - 1]--;
                flag = i;
            }
        }
        for (int i = flag; i < input.length(); i++) {
            array[i] = '9';
        }
        return Integer.parseInt(String.valueOf(array));
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int increasingDigits = new LeetCode738().monotoneIncreasingDigits(n);
            System.out.println(increasingDigits);
        }
    }
}
